package com.objectproject.main.service.system;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.objectproject.main.dao.MyObjectDAOImpl;
import com.objectproject.main.dao.MyPrimitiveDAOImpl;
import com.objectproject.main.enumeration.ValueType;
import com.objectproject.main.model.MyObject;
import com.objectproject.main.model.MyProperty;
import com.objectproject.main.model.primitive.MyPrimitiveCollection;
import com.objectproject.main.model.primitive.MyPrimitiveDate;
import com.objectproject.main.model.primitive.MyPrimitiveImage;
import com.objectproject.main.model.primitive.MyPrimitiveInteger;
import com.objectproject.main.model.primitive.MyPrimitiveReal;
import com.objectproject.main.model.primitive.MyPrimitiveText;
import com.objectproject.main.model.primitive.MyPrimitiveTime;

@Service
public class DetailValueLoader {
	
	@Autowired
	MyObjectDAOImpl myObjectDAOImpl;
	
	@Autowired
	MyPrimitiveDAOImpl myPrimitiveDAOImpl;
	
	public void loadAllPropertyValuesOf(MyObject myObject, int localeId){
		ArrayList<MyProperty> myProperties = myObject.getMyProperties();
		MyProperty currentProperty = null;
		long currentPropertyValueId = 0L;
		
		for(int i = 0 ; i < myProperties.size() ; i++){
			currentProperty = myProperties.get(i);
			currentPropertyValueId = currentProperty.getValueId();
			
			if(currentPropertyValueId == 0L){//속성 값이 정의되어있지 않다면
				continue;
			}
			
			if(currentProperty.getTypeOfValue() == ValueType.COLLECTION){
				MyPrimitiveCollection myPrimitiveCollection = myPrimitiveDAOImpl.selectMyPrimitiveCollection(currentPropertyValueId);
				myProperties.get(i).setValue(myPrimitiveCollection);
			}else if(currentProperty.getTypeOfValue() == ValueType.DATE){
				MyPrimitiveDate myPrimitiveDate = myPrimitiveDAOImpl.selectMyPrimitiveDate(currentPropertyValueId);
				myProperties.get(i).setValue(myPrimitiveDate);
			}else if(currentProperty.getTypeOfValue() == ValueType.IMAGE){
				
			}else if(currentProperty.getTypeOfValue() == ValueType.INTEGER){
				MyPrimitiveInteger myPrimitiveInteger = myPrimitiveDAOImpl.selectMyPrimitiveInteger(currentPropertyValueId);
				myProperties.get(i).setValue(myPrimitiveInteger);
			}else if(currentProperty.getTypeOfValue() == ValueType.REAL){
				MyPrimitiveReal myPrimitiveReal = myPrimitiveDAOImpl.selectMyPrimitiveReal(currentPropertyValueId);
				myProperties.get(i).setValue(myPrimitiveReal);
			}else if(currentProperty.getTypeOfValue() == ValueType.TEXT){
				MyPrimitiveText myPrimitiveText = myPrimitiveDAOImpl.selectMyPrimitiveText(currentPropertyValueId);
				myProperties.get(i).setValue(myPrimitiveText);
			}else if(currentProperty.getTypeOfValue() == ValueType.TIME){
				MyPrimitiveTime myPrimitiveTime = myPrimitiveDAOImpl.selectMyPrimitiveTime(currentPropertyValueId);
				myProperties.get(i).setValue(myPrimitiveTime);
			}else if(currentProperty.getTypeOfValue() == ValueType.LOCATION){
				
			}else if(currentProperty.getTypeOfValue() == ValueType.SOUND){
				
			}else if(currentProperty.getTypeOfValue() == ValueType.VIDEO){
				
			}else if(currentProperty.getTypeOfValue() == ValueType.OBJECT){
				MyObject curreuntPropertyValueObject = myObjectDAOImpl.selectMyObjectById(currentPropertyValueId);
				myProperties.get(i).setValue(curreuntPropertyValueObject);
			}
		}
	}
	
	/**
	 * helper of loading method
	 * @param myObject
	 */
	public Object loadPropertyHelper(ValueType valueType, MyProperty injectedProperty){
		//if objectId or primitiveId is zero, it will return null.
		if(valueType == ValueType.INTEGER){
			return myPrimitiveDAOImpl.selectMyPrimitiveInteger(injectedProperty.getValueId());
		}else if(valueType == ValueType.TEXT){
			return myPrimitiveDAOImpl.selectMyPrimitiveText(injectedProperty.getValueId());
		}else if(valueType == ValueType.COLLECTION){
			return myPrimitiveDAOImpl.selectMyPrimitiveCollection(injectedProperty.getValueId());
		}else if(valueType == ValueType.DATE){
			return myPrimitiveDAOImpl.selectMyPrimitiveDate(injectedProperty.getValueId());
		}else if(valueType == ValueType.TIME){
			
		}else if(valueType == ValueType.IMAGE){
			
		}else if(valueType == ValueType.REAL){
			
		}else if(valueType == ValueType.LOCATION){
			
		}else if(valueType == ValueType.SOUND){
			
		}else if(valueType == ValueType.VIDEO){
			
		}else if(valueType == ValueType.OBJECT){
			return myObjectDAOImpl.selectMyObjectById(injectedProperty.getValueId());
		}
		return null;
	}
	
	/**
	 * set only primitive value of primitive-property
	 * @param myObject
	 */
	public void loadPrimitivePropertyValuesOf(MyObject myObject){
		
	}
	
	public void loadMinimizedPropertyValuesOf(){
		
	}
	public void loadSimplifiedPropertyValuesOf(){
		
	}
	public void loadCardPropertyValuesOf(){
		
	}
	
	public void loadCollectionElementValuesOf(MyPrimitiveCollection myPrimitiveCollection){
		
	}
}
